package com.allwees.bs.module.oms.rest;

import com.allwees.bs.module.core.annotation.PrintLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.dynamic.annotation.DynamicInject;
import com.allwees.bs.module.core.dynamic.annotation.MultiDynamicInject;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.entity.OrderRefund;
import com.allwees.bs.module.oms.req.query.OmsOrderItemQueryReq;
import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
import com.allwees.bs.module.oms.vo.TrackingDetailVo;
import com.allwees.core.common.constant.EStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@PrintLog
@RestController
@RequestMapping("/backstage/api/oms/oms_order_item")
public class GeneralOmsOrderItemApi extends BaseOmsOrderItemApi<GeneralOmsOrderItemService> {

    /**
     * B端 查询订单
     *
     * @return
     */
    @MultiDynamicInject(value = {
            @DynamicInject(type = DynamicInject.Type.Retailer, key = "retailerUuid"),
            @DynamicInject(type = DynamicInject.Type.Logistics, key = "logisticsUuid"),
            @DynamicInject(type = DynamicInject.Type.ShippingAddress, key = "orderUuid"),
            @DynamicInject(key = "uuid")
    })
    @GetMapping("/page")
    public R page(Page<OmsOrderItem> page, OmsOrderItemQueryReq req) {
        if (EBranchType.RETAILER.equals(getCurrentUser().getBranchType())) {
            req.setRetailerUuid(getCurrentUser().getRetailerUuid());
        }
        QueryWrapper<OmsOrderItem> wrapper = new QueryWrapper<>();
        req.buildQuery(wrapper);
        return super.page(page, req);
    }

    /**
     * 根据订单uuid 查询订单
     *
     * @param orderUuid
     * @return
     */
    @MultiDynamicInject(value = {
            @DynamicInject(type = DynamicInject.Type.Retailer, key = "retailerUuid"),
            @DynamicInject(key = "uuid")
    })
    @GetMapping("/find_by_order_uuid/{orderUuid}")
    public R findByOrderUuid(@PathVariable String orderUuid) {
        List<OmsOrderItem> orderItems = boService.findByOrderUuid(orderUuid);
        return R.ok(orderItems);
    }

    /**
     * 查询待结算订单项
     *
     * @param page
     * @param req
     * @return
     */
    @GetMapping("/to_settle")
    public R toSettle(Page<OmsOrderItem> page, OmsOrderItemQueryReq req) {
        req.setStatus(EStatus.OMS_TO_SETTLE);
        IPage<OmsOrderItem> retPage = boService.findPage(page, req);
        return R.ok(retPage);
    }

    /**
     * 查询已结算订单项
     *
     * @param page
     * @param req
     * @return
     */
    @GetMapping("/settled")
    public R settled(Page<OmsOrderItem> page, OmsOrderItemQueryReq req) {
        req.setStatus(EStatus.OMS_SETTLED);
        IPage<OmsOrderItem> retPage = boService.findPage(page, req);
        return R.ok(retPage);
    }

    /**
     * 查看物流进度信息
     *
     * @param orderItemUuid
     * @return
     */
    @GetMapping("/tracking-detail/{order_item_uuid}")
    public R queryTrackingDetail(@PathVariable("order_item_uuid") String orderItemUuid) {
        TrackingDetailVo trackingDetailVo = boService.queryTrackingDetail(orderItemUuid);
        return R.ok(trackingDetailVo);
    }
}
